/**
 * 
 */
package com.wrsr.examsys.teacher.action;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wrsr.examsys.domain.College;
import com.wrsr.examsys.domain.Provinces;
import com.wrsr.examsys.domain.Resouce;
import com.wrsr.examsys.domain.Teacher;
import com.wrsr.examsys.domain.University;
import com.wrsr.examsys.resouce.service.ResouceService;
import com.wrsr.examsys.teacher.service.CollegeService;
import com.wrsr.examsys.teacher.service.ProvincesService;
import com.wrsr.examsys.teacher.service.TeacherService;
import com.wrsr.examsys.teacher.service.UniversityService;
import com.wrsr.examsys.util.FilePath;
import com.wrsr.examsys.util.MD5;
import com.wrsr.examsys.util.PptToImage;
import com.wrsr.examsys.valited.StringIsValitedUtils;

/**
 * @author <a href="mailto:2397431783@qq.com">wangruishun</a>
 * @version examsy1.0 2015年1月7日 下午9:40:06
 */
@Controller("teacherAction")
@Scope("prototype")
public class TeacherAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware, ServletContextAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2741385404372190772L;
	@Resource
	private ProvincesService provincesService;
	@Resource
	private UniversityService universityService;
	@Resource
	private TeacherService teacherService;
	@Resource
	private CollegeService collegeService;
	@Resource
	private ResouceService resouceService;
	private List<Provinces> provincesList;
	private List<University> universityList;
	private List<College> collegeList;
	private Integer provinceId;
	private Integer universityId;
	private String repwd;
	private String pid;
	private String uid;
	private String cid;
	private String message = "";
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Teacher teacher = new Teacher();
	private File resoucepath;
	private String resoucepathFileName;
	private ServletContext sc;
	private Resouce resouce = new Resouce();
	private List<Resouce> resList;
	private String resouceId;
	private Integer imageCount = 1;
	private String imagePath;
	// 前台展示ppt 1:加
	private Integer showPPTFlag1 = 1;

	private Integer showPPTFlag = 0;

	public String teacher() {
		System.out.println("0------------:"+request.getRemoteAddr());
		System.out.println("0------------:"+request.getLocalName());
		System.out.println("0------------:"+request.getRemoteHost());
		return SUCCESS;
	}

	public String teacherLogin() {
		return SUCCESS;
	}

	public String teacherLoginBeg() {
		if (teacherService.getTeacherByEmailAndPwd(teacher)) {
			teacher = teacherService.getEntity(teacher.getEmail());
			request.getSession().setAttribute("teacher", teacher);
			return SUCCESS;
		} else {
			message = "*亲，用户名和密码不匹配";
			return "LoginWrong";
		}
	}

	public String exitTeacher() {
		request.getSession().setAttribute("teacher", "");
		request.getSession().removeAttribute("teacher");

		return SUCCESS;
	}

	public String teacherRegist() {
		provincesList = provincesService.getProvincesAll();
		return SUCCESS;
	}

	public String getUniversityByProvinceId() {
		universityList = universityService
				.getUniversityByProvinceId(provinceId);
		return SUCCESS;
	}

	public String getCollegeByUniversityId() {
		collegeList = collegeService.getCollegeByUniversityId(universityId);
		return SUCCESS;
	}

	public String saveTeacher() {
		teacher.setPwd(MD5.GetMD5Code(teacher.getPwd().trim()));
		teacher.setCollegeId(String.valueOf(cid));
		teacher.setUniversityId(String.valueOf(uid));
		teacher.setProvinceId(String.valueOf(pid));
		if (teacherService.emailIsExist(teacher)) {
			message = "邮箱已经存在了，亲！";
			return "wrong";
		} else {
			teacherService.saveEntity(teacher);
			return SUCCESS;
		}
	}

	public String resoucePage() {
		return SUCCESS;
	}

	/**
 * 
 */
	public String reouceUpLoade() {

		if (StringIsValitedUtils.isValid(resoucepathFileName)
				&& StringIsValitedUtils.isValid(resouce.getResouceName())
				&& StringIsValitedUtils.isValid(resouce.getSubject())) {
			String dir = sc.getRealPath("/upload");
			final long l = System.nanoTime();
			final String ext = resoucepathFileName
					.substring(resoucepathFileName.lastIndexOf("."));
			final String strPath = dir + "/" + FilePath.getUpLoadPath(l + ext);
			File f = new File(strPath);
			if (!f.exists()) {
				f.mkdirs();
			}
			File newFile = new File(strPath, l + ext);
			resoucepath.renameTo(newFile);
			resouce.setImagePath(strPath);
			resouce.setPath(strPath + "/" + l + ext);
			Teacher teacher = (Teacher) request.getSession().getAttribute(
					"teacher");
			resouce.setTeacherId(teacher.getIdcard());
			if (".ppt".equals(ext) || ".pptx".equals(ext)) {
				resouce.setType("1");

				int length = PptToImage.getImage(resouce);
				resouce.setImagePath("/upload/"
						+ FilePath.getUpLoadPath(l + ext));
				resouce.setPath("/upload/" + FilePath.getUpLoadPath(l + ext)
						+ "/" + l + ext);
				resouce.setImageLength(length);
				resouceService.saveEntity(resouce);

			} else if (".mp4".equals(ext)) {
				resouce.setPath("/upload/" + FilePath.getUpLoadPath(l + ext)
						+ "/" + l + ext);
				resouce.setType("2");
				resouceService.saveEntity(resouce);
			} else {
				resouce.setPath("/upload/" + FilePath.getUpLoadPath(l + ext)
						+ "/" + l + ext);
				resouce.setType("0");
				resouceService.saveEntity(resouce);
			}
			return SUCCESS;
		} else
			return "input";
	}

	public String getResouceList() {
		String hql = "from Resouce r where r.deleteFlage=? and r.teacherId=?";
		teacher = (Teacher) request.getSession().getAttribute("teacher");
		resList = resouceService.findEntityByHQL(hql, 1, teacher.getIdcard());
		return SUCCESS;
	}

	public String getAllResouceList() {
		String hql = "from Resouce r where r.deleteFlage=?";
		resList = resouceService.findEntityByHQL(hql, 1);
		return SUCCESS;
	}

	public String pptShow() {
		resouce = resouceService.getEntity(resouceId);
		if (showPPTFlag == 1) {
			showPPTFlag1--;
			if (showPPTFlag1 <= 0)
				showPPTFlag1 = 1;
			imagePath = resouce.getImagePath() + "/" + showPPTFlag1 + ".png";
			return SUCCESS;
		}
		if (showPPTFlag == 2) {
			showPPTFlag1++;
			if (showPPTFlag1 >= resouce.getImageLength())
				showPPTFlag1 = resouce.getImageLength();
			imagePath = resouce.getImagePath() + "/" + showPPTFlag1 + ".png";
			return SUCCESS;
		} else {
			imagePath = resouce.getImagePath() + "/" + 1 + ".png";
		}
		return SUCCESS;
	}

	public String deleteResouce() {
		resouce = resouceService.getEntity(resouceId);
		resouce.setDeleteFlage(0);
		resouceService.updateEntity(resouce);
		return SUCCESS;
	}

	// 下载文件
	private String fileName;

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileName() {
		return this.fileName;
	}

	public InputStream getInputStream() {
		return ServletActionContext.getServletContext().getResourceAsStream(
				"/" + fileName);
	}

	public String execute() {
		return SUCCESS;
	}

	/**
	 * @return the universityList
	 */
	public List<University> getUniversityList() {
		return universityList;
	}

	/**
	 * @param universityList
	 *            the universityList to set
	 */
	public void setUniversityList(List<University> universityList) {
		this.universityList = universityList;
	}

	/**
	 * @return the provincesList
	 */
	public List<Provinces> getProvincesList() {
		return provincesList;
	}

	/**
	 * @param provincesList
	 *            the provincesList to set
	 */
	public void setProvincesList(List<Provinces> provincesList) {
		this.provincesList = provincesList;
	}

	/**
	 * @return the provinceId
	 */
	public Integer getProvinceId() {
		return provinceId;
	}

	/**
	 * @param provinceId
	 *            the provinceId to set
	 */
	public void setProvinceId(Integer provinceId) {
		this.provinceId = provinceId;
	}

	/**
	 * @return the collegeList
	 */
	public List<College> getCollegeList() {
		return collegeList;
	}

	/**
	 * @param collegeList
	 *            the collegeList to set
	 */
	public void setCollegeList(List<College> collegeList) {
		this.collegeList = collegeList;
	}

	/**
	 * @return the universityId
	 */
	public Integer getUniversityId() {
		return universityId;
	}

	/**
	 * @param universityId
	 *            the universityId to set
	 */
	public void setUniversityId(Integer universityId) {
		this.universityId = universityId;
	}

	/**
	 * @return the repwd
	 */
	public String getRepwd() {
		return repwd;
	}

	/**
	 * @param repwd
	 *            the repwd to set
	 */
	public void setRepwd(String repwd) {
		this.repwd = repwd;
	}

	
	
	/**
	 * @return the pid
	 */
	public String getPid() {
		return pid;
	}

	/**
	 * @param pid the pid to set
	 */
	public void setPid(String pid) {
		this.pid = pid;
	}

	/**
	 * @return the uid
	 */
	public String getUid() {
		return uid;
	}

	/**
	 * @param uid the uid to set
	 */
	public void setUid(String uid) {
		this.uid = uid;
	}

	/**
	 * @return the cid
	 */
	public String getCid() {
		return cid;
	}

	/**
	 * @param cid the cid to set
	 */
	public void setCid(String cid) {
		this.cid = cid;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	/**
	 * @param teacher
	 *            the teacher to set
	 */
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletResponseAware#setServletResponse
	 * (javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response=arg0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
	 * javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	/**
	 * @return the resoucepath
	 */
	public File getResoucepath() {
		return resoucepath;
	}

	/**
	 * @param resoucepath
	 *            the resoucepath to set
	 */
	public void setResoucepath(File resoucepath) {
		this.resoucepath = resoucepath;
	}

	/**
	 * @return the resoucepathName
	 */
	public String getResoucepathFileName() {
		return resoucepathFileName;
	}

	/**
	 * @param resoucepathName
	 *            the resoucepathName to set
	 */
	public void setResoucepathFileName(String resoucepathFileName) {
		this.resoucepathFileName = resoucepathFileName;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;
	}

	/**
	 * @return the resouce
	 */
	public Resouce getResouce() {
		return resouce;
	}

	/**
	 * @param resouce
	 *            the resouce to set
	 */
	public void setResouce(Resouce resouce) {
		this.resouce = resouce;
	}

	/**
	 * @return the resList
	 */
	public List<Resouce> getResList() {
		return resList;
	}

	/**
	 * @return the resouceId
	 */
	public String getResouceId() {
		return resouceId;
	}

	/**
	 * @param resouceId
	 *            the resouceId to set
	 */
	public void setResouceId(String resouceId) {
		this.resouceId = resouceId;
	}

	/**
	 * @param resList
	 *            the resList to set
	 */
	public void setResList(List<Resouce> resList) {
		this.resList = resList;
	}

	/**
	 * @return the imageCount
	 */
	public Integer getImageCount() {
		return imageCount;
	}

	/**
	 * @param imageCount
	 *            the imageCount to set
	 */
	public void setImageCount(Integer imageCount) {
		this.imageCount = imageCount;
	}

	/**
	 * @return the imagePath
	 */
	public String getImagePath() {
		return imagePath;
	}

	/**
	 * @param imagePath
	 *            the imagePath to set
	 */
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	/**
	 * @return the showPPTFlag1
	 */
	public Integer getShowPPTFlag1() {
		return showPPTFlag1;
	}

	/**
	 * @param showPPTFlag1
	 *            the showPPTFlag1 to set
	 */
	public void setShowPPTFlag1(Integer showPPTFlag1) {
		this.showPPTFlag1 = showPPTFlag1;
	}

	/**
	 * @return the showPPTFlag
	 */
	public Integer getShowPPTFlag() {
		return showPPTFlag;
	}

	/**
	 * @param showPPTFlag
	 *            the showPPTFlag to set
	 */
	public void setShowPPTFlag(Integer showPPTFlag) {
		this.showPPTFlag = showPPTFlag;
	}

}
